﻿using Plane.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Plane
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void textBox_password_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //阻止按键默认行为
                e.SuppressKeyPress = true;
                string username = textBox_username.Text.Trim();
                string input_password= textBox_password.Text.Trim();
                var User=UserDbHelper.GetUserInformationByUsername(username);
                //用户不存在时返回的密码为空，密码不为空则代表用户存在
                if (!(string.IsNullOrEmpty(User.Password)))
                {
                    string correct_password = User.Password; //获取密码
                    //密码不正确
                    if (correct_password != input_password)
                    {
                        MessageBox.Show("密码不正确，请重新输入",
                        "登录失败",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning);
                        textBox_password.Clear(); // 清空密码框
                        textBox_password.Focus(); // 聚焦到密码框
                    }
                    else
                    {
                        //登录成功则跳转到用户界面
                        MainWindow mainWindow = new MainWindow(username);
                        mainWindow.Show();
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("该用户不存在，请检查用户名或注册新账号",
                      "登录失败",
                      MessageBoxButtons.OK,
                      MessageBoxIcon.Warning);
                    textBox_username.Clear();
                    textBox_username.Focus(); // 将焦点返回用户名输入框
                }
            }
        }

        private void button_return_Click(object sender, EventArgs e)
        {
            Login_Register login_Register = new Login_Register();

            login_Register.Show();

            this.Close();
        }

        private void button_ensure_Click(object sender, EventArgs e)
        {
            string username = textBox_username.Text.Trim();
            string input_password = textBox_password.Text.Trim();
            var User = UserDbHelper.GetUserInformationByUsername(username);
            if (!(string.IsNullOrEmpty(User.Password)))
            {
                string correct_password = User.Password; //获取密码
                                                               //密码不正确
                if (correct_password != input_password)
                {
                    MessageBox.Show("密码不正确，请重新输入",
                    "登录失败",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
                    textBox_password.Clear(); // 清空密码框
                    textBox_password.Focus(); // 聚焦到密码框
                }
                else
                {
                    //登录成功则跳转到用户界面
                    MainWindow mainWindow = new MainWindow(username);
                    mainWindow.Show();
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("该用户不存在，请检查用户名或注册新账号",
                  "登录失败",
                  MessageBoxButtons.OK,
                  MessageBoxIcon.Warning);
                textBox_username.Clear();
                textBox_password.Clear();
                textBox_username.Focus(); // 将焦点返回用户名输入框
            }
        }


        private bool isPasswordVisible = false;

        //密码的显示与隐藏
        private void button_show_Click(object sender, EventArgs e)
        {
            isPasswordVisible = !isPasswordVisible;
            //true可见，false不可见
            textBox_password.PasswordChar = isPasswordVisible ? '\0' : '*';

            if (isPasswordVisible)
            {
                button_show.Image = Plane.Properties.Resources.OIP_1__2__1__1_;
            }
            else
            {
                button_show.Image = Plane.Properties.Resources.close;
            }
        }
    }
}
